﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SharpLite.Domain.DataInterfaces;
using DAEProde.Domain;
using DAEProde.Tasks;

namespace DAEProde.Web.Controllers
{
    [Authorize(Roles = "Administrator")]
    public class TeamManagerController : Controller
    {
        private IRepository<Team> _teamRepository;
        private TeamCudTasks _teamMgmtTasks;

        public TeamManagerController(IRepository<Team> teamRepository, TeamCudTasks teamMgmtTasks)
        {
            _teamRepository = teamRepository;
            _teamMgmtTasks = teamMgmtTasks;
        }

        //
        // GET: /TeamManager/

        public ActionResult Index()
        {
            var x = _teamRepository.GetAll();

            return View(x.OrderBy(c => c.Name));
        }

        public ActionResult Create()
        {
            return View("Edit", _teamMgmtTasks.CreateEditViewModel());
        }

        public ActionResult Details(int id)
        {
            var x = _teamRepository.Get(id);
            return View(x);
        }

        public ActionResult Edit(int id)
        {
            return View(_teamMgmtTasks.CreateEditViewModel(id));
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Team team)
        {
            if (ModelState.IsValid)
            {
                ActionConfirmation<Team> confirmation = _teamMgmtTasks.SaveOrUpdate(team);

                if (confirmation.WasSuccessful)
                {
                    TempData["message"] = confirmation.Message;
                    return RedirectToAction("Index");
                }

                ViewData["message"] = confirmation.Message;
            }

            return View(_teamMgmtTasks.CreateEditViewModel(team));
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Delete(int id)
        {
            ActionConfirmation<Team> confirmation = _teamMgmtTasks.Delete(id);
            TempData["message"] = confirmation.Message;
            return RedirectToAction("Index");
        }
    }
}
